﻿using System.Diagnostics;
using System.Reflection;
using System.Text.RegularExpressions;
using Bzstd.Entity;

namespace Bzstd.Utility;

/// <summary>
/// 配置文件操作
/// </summary>
public static class ConfigHelper
{
	/// <summary>
	/// 入口程序集路径
	/// </summary>
	public static string ProductPath { get; } = AppContext.BaseDirectory;

	/// <summary>
	/// 入口程序集
	/// </summary>
	public static Assembly ProductAssembly { get; } = Assembly.GetExecutingAssembly();

	/// <summary>
	/// 静态资源程序集
	/// </summary>
	public static Assembly StaticAssembly { get; } = typeof(StaticAssemblyRef).Assembly;

	/// <summary>
	/// 命令行参数
	/// </summary>
	public static string[] CommandLineArgs { get; } = Environment.GetCommandLineArgs();

	/// <summary>
	/// 产品命名空间
	/// </summary>
	public static string ProductNamespace { get; } = typeof(Program).Namespace;

	/// <summary>
	/// 产品名称
	/// </summary>
	public static string ProductName { get; } = ProductAssembly.GetCustomAttribute<AssemblyProductAttribute>()?.Product;

	/// <summary>
	/// 产品简称
	/// </summary>
	public static string ProductShortName { get; } = ProductAssembly.GetCustomAttribute<AssemblyDescriptionAttribute>()?.Description;

	/// <summary>
	/// 源代码路径
	/// </summary>
	public static string SourcePath { get; } = Path.GetFullPath(Path.Combine(new StackTrace(true).GetFrames().Last().GetFileName() ?? string.Empty, "..", ".."));

	/// <summary>
	/// 静态资源源代码路径
	/// </summary>
	public static string StaticSourcePath { get; } = $"{SourcePath}.Static";

	/// <summary>
	/// 是否是Debug模式
	/// </summary>
	#if DEBUG
	public static bool IsDebugMode => true;
	#else
	public static bool IsDebugMode => false;
	#endif

	/// <summary>
	/// 回车换行符（CRLF）常量
	/// </summary>
	public static string CrLf => "\r\n";

	/// <summary>
	/// 路径分隔符数组
	/// </summary>
	public static char[] PathSeparators { get; } = @"/\".ToCharArray();

	/// <summary>
	/// 路径分隔符正则表达式
	/// </summary>
	public static Regex PathSeparatorRegex { get; } = new(@"[/\\]+", RegexOptions.Compiled);

	/// <summary>
	/// ASCII或中文字符正则表达式
	/// </summary>
	public static Regex AsciiOrChineseRegex { get; } = new(@"[\x20-\x7e\u4e00-\u9fa5]+", RegexOptions.Compiled);

	/// <summary>
	/// 字符串分割选项
	/// </summary>
	public static StringSplitOptions TrimAndRemoveEmptyEntries => StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries;

	/// <summary>
	/// 游戏信息
	/// </summary>
	public static GameInfo GameInfo { get; } = GameInfo.LoadFromCommandLineArgs();
}
